package com.test.fundible.fundible_05_process.widget;

import static com.test.fundible.fundible_00_utils.FundibleUtil.startLoading;
import static com.test.fundible.fundible_00_utils.FundibleUtil.stopLoading;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.test.fundible.R;
import com.test.fundible.fundible_00_utils.FundibleUtil;
import com.test.fundible.fundible_01_launcher.App_fundibleShow_fundibleCustomize;
import com.test.fundible.fundible_01_launcher.Base_fundibleApplication;
import com.test.fundible.fundible_05_process.Proces_fundiblesIn_fundiblefo_Data;
import com.test.fundible.fundible_05_process.Acc_fundibleountLi_fundiblest_Fr;
import com.test.fundible.fundible_05_process.Ba_fundiblenkAcc_fundibleount_Fr;
import com.test.fundible.Othe_fundiblersInfo;
import com.test.fundible.fundible_05_process.Bin_fundibledAcco_fundibleuntChannelDialog_Fr;
import com.test.fundible.fundible_05_process.Bin_fundibledAccount_fundibleDialog_Fr;
import com.test.fundible.fundible_05_process.Delete_fundibleAccountDi_fundiblealog_Fr;
import com.test.fundible.fundible_00_http.OkHttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class BindAcco_fundibleuntViewM_fundibleanager_Fr extends BaseProc_fundibleessWid_fundiblegetManager {

    private final FragmentActivity activ_fundibleity;

    private LinearLayout accoun_fundibletGroup_fundibleLayout;

    private ArrayList<Ba_fundiblenkAcc_fundibleount_Fr> accoun_fundiblets = new ArrayList<>();

    private final View.OnClickListener addAcco_fundibleuntCl;

    private String bindChann_fundibleelMobile = "";

    public BindAcco_fundibleuntViewM_fundibleanager_Fr(View view, Proces_fundiblesIn_fundiblefo_Data data, String pageTag){
        super(view,data,pageTag);
        activ_fundibleity = (FragmentActivity) view.getContext();
        addAcco_fundibleuntCl = v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG,"ADD_CLICK","");
            new Bin_fundibledAccount_fundibleDialog_Fr(activ_fundibleity, TAG,
                    mobile -> {
                        bindChann_fundibleelMobile = mobile;
                        OkHttpUtil.getInstance_fundible().fetchWalletAccountList_fr(this::refreshAcc_fundibleountList);
                    }
            ).show();
        };
     }

    @Override
    protected void in_fundibleit(){
        accoun_fundibletGroup_fundibleLayout = mView.findViewById(R.id.ipbfa_layout_account_group);
        TextView tipsTitleTv_fundible = mView.findViewById(R.id.ipbfa_tv_tips_title);
        TextView tipsDescTv_fundible = mView.findViewById(R.id.ipbfa_tv_tips_desc);

        mView.findViewById(R.id.ipbfa_btn_add_account).setOnClickListener(v -> {
            addAcco_fundibleuntCl.onClick(v);
        });

        String tipsTitle = App_fundibleShow_fundibleCustomize.getInstance_fundible().getBindAccountTipsTitle_fundible();
        String tipsDesc = App_fundibleShow_fundibleCustomize.getInstance_fundible().getBindAccountTipsDesc_fundible();

        if (!TextUtils.isEmpty(tipsTitle) && !TextUtils.isEmpty(tipsDesc)){
            tipsTitleTv_fundible.setText(tipsTitle);
            tipsDescTv_fundible.setText(tipsDesc);
        }

    }

    @Override
    public boolean checkI_fundiblenput() {
        return !accoun_fundiblets.isEmpty();
    }

    @Override
    public String getInp_fundibleut() {
        return String.valueOf(accoun_fundiblets.size());
    }

    //更新列表
    public void refreshAcc_fundibleountList(){

        stopLoading();

        accoun_fundibletGroup_fundibleLayout.removeAllViews();

        accoun_fundiblets = Acc_fundibleountLi_fundiblest_Fr.getInstance_fundible().getBankAccountList_fundible();

        int num = 1;
        int i= accoun_fundiblets.size()-1;
        while (i> accoun_fundiblets.size()-3 || i>=0){

            if (i>=0) addAcc_fundibleount(accoun_fundiblets.get(i),num++);
            else {
                View view = View.inflate(mView.getContext(), R.layout.item_add_fundible_account_fr, null);
                TextView titleView = view.findViewById(R.id.iaafa_tv_title);
                titleView.setText(Base_fundibleApplication.getInstant().getString(R.string.bind_account_mobile,num++));
                view.findViewById(R.id.iaafa_view_click).setOnClickListener(addAcco_fundibleuntCl);
                accoun_fundibletGroup_fundibleLayout.addView(view);
            }

            i--;
        }

        //更新列表后，判断是否需要绑定运营商
        if (!TextUtils.isEmpty(bindChann_fundibleelMobile) ){
            if (Othe_fundiblersInfo.getInstance_fundible().getAddBindFlag_fundible()){
                new Bin_fundibledAcco_fundibleuntChannelDialog_Fr(activ_fundibleity, bindChann_fundibleelMobile,
                        null,
                        () -> {
                            startLoading(activ_fundibleity,"");
                            OkHttpUtil.getInstance_fundible().addWalletAccount_Fr(
                                    bindChann_fundibleelMobile,
                                    bindChann_fundibleelMobile,
                                    Othe_fundiblersInfo.getInstance_fundible().getChannel_fundible(),
                                    () -> OkHttpUtil.getInstance_fundible().fetchWalletAccountList_fr(this::refreshAcc_fundibleountList),
                                    false
                            );
                        }

                ).show();
            }
        }

    }

    //添加卡
    private void addAcc_fundibleount(Ba_fundiblenkAcc_fundibleount_Fr account, int num){

        if (account == null) return;

        String desc = account.getWalletDesc_fundible();

        View accountView = View.inflate(mView.getContext(), R.layout.item_fundible_account_fr, null);
        TextView titleTv = accountView.findViewById(R.id.iafa_tv_title);
        ImageView logoIv = accountView.findViewById(R.id.iafa_iv_logo);
        TextView channelTv = accountView.findViewById(R.id.iafa_tv_channel);
        TextView mobileTv = accountView.findViewById(R.id.iafa_tv_mobile);
        ImageButton deleteIb = accountView.findViewById(R.id.iafa_ib_delete_account);

        titleTv.setText(Base_fundibleApplication.getInstant().getString(R.string.bind_account_mobile,num));
        mobileTv.setText(account.getWalletMobile_fundible());
        channelTv.setText(account.getWalletDesc_fundible());

        //删除卡
        deleteIb.setOnClickListener(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG,"DELETE_CLICK",account.getWalletMobile_fundible());
            new Delete_fundibleAccountDi_fundiblealog_Fr(activ_fundibleity,account, TAG,
                    () -> OkHttpUtil.getInstance_fundible().fetchWalletAccountList_fr(this::refreshAcc_fundibleountList)).show();
        });

        accoun_fundibletGroup_fundibleLayout.addView(accountView);

        //获取图标
        if (App_fundibleShow_fundibleCustomize.getInstance_fundible().getChannelLogoByDesc_fundible(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo_fundible())) return;
                    if (!App_fundibleShow_fundibleCustomize.getInstance_fundible().hasAccountChannel_fundible()) return;
                    URL url = new URL(account.getWalletLogo_fundible());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        App_fundibleShow_fundibleCustomize.getInstance_fundible().setChannelLogoByDesc_fundible(desc,bitmap);
                        FundibleUtil.safeRun(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(App_fundibleShow_fundibleCustomize.getInstance_fundible().getChannelLogoByDesc_fundible(desc));

    }

}
